Omplus.base.define(Omplus.controllers.Controller, {
    omplusName:"alertsController"
});

Omplus.models.AlertProto = Omplus.base.extend(Omplus.models.Model, {
    omplusType:"AlertProto",
    value:{
        "code":{
            omplusType:"String"
        },
        "is_resolved":{
            omplusType:"Boolean"
        },
        "alert_id":{
            omplusType:"Number"
        },
        "is_read":{
            omplusType:"Boolean"
        },
        "datetime_added":{
            omplusType:"DateTime"
        },
        "player_id":{
            omplusType:"Number"
        }
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"Alerts",
    controller:"alertsController",
    value:{
        new_alerts:{
            omplusType:"Array",
            omplusProto:"AlertProto"
        },
        old_alerts:{
            omplusType:"Array",
            omplusProto:"AlertProto"
        },
        alerts:{
            omplusType:"Array",
            omplusProto:"AlertProto"
        },
        total:{
            omplusType:"Number"
        }
    },
    actions: [
        {
            omplusType:"Get",
            omplusName:"getAlerts",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"UserShort",
            responseModel:"Alerts",
            callback:function () {
                setTimeout('Omplus.globals.getObject("Alerts").getAction("getAlerts").run()', 10000);
            }
        }
    ]
});


Omplus.base.define(Omplus.models.AlertProto, {
    omplusName:"AlertsManipulation",
    actions: [
        {
            omplusType:"Get",
            omplusName:"setResolved",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"AlertsManipulation",
            responseModel:"Alerts"
        },
        {
            omplusType:"Get",
            omplusName:"setRead",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"AlertsManipulation",
            responseModel:"Alerts"
        },
        {
            omplusType:"Get",
            omplusName:"getPlayerDetails",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"AlertsManipulation",
            responseModel:"playerChooser.current",
            callback : function(){
                Omplus.globals.getObject("playerChooser").addToList(Omplus.globals.getObject("playerChooser.current").getValue());
                Omplus.globals.getObject("playersPanel").getAction("setInStorage").run();
                Omplus.globals.getObject("navbar_1_big").click();
                setTimeout('Omplus.globals.getObject("playersPanel.Layout").activate(-1)', 500)
            }
        }
    ]
});

Omplus.views.AlertsRow = Omplus.base.extend(Omplus.views.TableRow, {
    omplusType:"AlertsRow",
    components:[
        {
            omplusType:"Label",
            omplusName:"code",
            model:"code",
            setValue : function(newValue){
                if (newValue == null) newValue = "";
                var model = Omplus.globals.getObject("Assets.list.text_for_alert" + newValue + ".localValue");
                if (model)
                    this.getContent().find("label").text(model.getValue());
            }
        },
        {
            omplusType:"Label",
            omplusName:"datetime_added",
            model:"datetime_added"
        },
        {
            omplusType:"Button",
            omplusName:"mark_resolved",
            model:"Assets.list.label_mark_resolved.localValue",
            controller:"languageController",
            click: function(){
                var array = $(this).closest(".tableRow").attr("id").split("_");
                var row = array[array.length - 1];
                Omplus.globals.getObject("AlertsManipulation").setValue(Omplus.globals.getObject("Alerts.old_alerts").getMemberByIndex(row).getValue());
                Omplus.globals.getObject("AlertsManipulation").getAction("setResolved").run();
            },
            init:function(){
                var row = this.index;
                var value = Omplus.globals.getObject("Alerts.old_alerts").getMemberByIndex(row).getValue();
                if (value.is_resolved){
                    this.cssClasses.push("importantHidden")
                }
            }
        },
        {
            omplusType:"Button",
            omplusName:"mark_read",
            model:"Assets.list.label_mark_read.localValue",
            controller:"languageController",
            click: function(){
                var array = $(this).closest(".tableRow").attr("id").split("_");
                var row = array[array.length - 1];
                Omplus.globals.getObject("AlertsManipulation").setValue(Omplus.globals.getObject("Alerts.old_alerts").getMemberByIndex(row).getValue());
                Omplus.globals.getObject("AlertsManipulation").getAction("setRead").run();
            },
            init:function(){
                var row = this.index;
                var value = Omplus.globals.getObject("Alerts.old_alerts").getMemberByIndex(row).getValue();
                if (value.is_read){
                    this.cssClasses.push("importantHidden")
                }
            }
        },
        {
            omplusType:"Button",
            omplusName:"get_player",
            model:"Assets.list.label_get_player.localValue",
            controller:"languageController",
            click: function(){
                var array = $(this).closest(".tableRow").attr("id").split("_");
                var row = array[array.length - 1];
                var player = Omplus.globals.getObject("Alerts.old_alerts").getMemberByIndex(row).getValue();
                Omplus.globals.getObject("playersPanel").getAction("getFromStorage").run();
                var players = Omplus.globals.getObject("playerChooser").getValue();
                var isset = false;
                var object = null;
                var index = null;
                for(var i in players.list){
                    if(players.list[i].player_id == player.player_id){
                        isset = true;
                        object = players.list[i];
                        index = i;
                        break;
                    }
                }
                if (isset){
                    Omplus.globals.getObject("playerChooser").removeByIndex(index);
                    Omplus.globals.getObject("playerChooser").addToList(object);
                    Omplus.globals.getObject("playersPanel").getAction("setInStorage").run();
                    Omplus.globals.getObject("navbar_1_big").click();
                    setTimeout('Omplus.globals.getObject("playersPanel.Layout").activate(-1)', 500)
                }else{
                    Omplus.globals.getObject("AlertsManipulation").setValue(player);
                    Omplus.globals.getObject("AlertsManipulation").getAction("getPlayerDetails").run();
                }
            }
        }
    ]
});

Omplus.base.define(Omplus.views.Panel, {
    omplusName:"alertsPanel",
    renderTo:"managements",
    models:{
    },
    components:[
        {
            omplusType:"TabPanel",
            omplusName:"alertsListPanel",
            layout:{
                omplusType:"HorizontalLayout"
            },
            controller:"languageController",
            model:"Assets.list.label_alerts.localValue",
            components:[
                {
                    omplusType:"Table",
                    omplusName:"alertsListTablePanel",
                    model :"Alerts.old_alerts",
                    controller:"alertsController",
                    omplusProto:"AlertsRow",
                    rowHeight:"35px",
                    rowCellWidths:[
                        "300px",
                        "150px",
                        "170px",
                        "170px",
                        "170px"
                    ]
                }
            ]
        }
    ],
    layout:{
        omplusType:"TabLayout"
    },
    showEffect:{
        name:"explode",
        time:300
    },
    hideEffect:{
        name:"explode",
        time:300
    }
});